﻿package com.dykj.apps.web.sysmanager.action;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.dykj.apps.web.core.BaseAction;
import com.dykj.apps.web.sysmanager.dto.FunctionDTO;
import com.dykj.apps.web.sysmanager.service.IFunctionBS;
import com.dykj.platform.framework.exception.ApplicationException;
import com.googlecode.jsonplugin.annotations.JSON;

public class FunctionAction  extends BaseAction{
	private List nodes;
	private IFunctionBS bs;
	private FunctionDTO functionDTO = new FunctionDTO();
	private String funMethod;

	/**
	 * 取得树形功能菜单
	 * @return
	 * @throws ApplicationException
	 */
	public String queryFunction() throws ApplicationException{
		Map info = (Map)this.getSession().getAttribute("userinfo");
		if(null!=info){
			String roleid=info.get("roleid").toString();
			String type = info.get("type").toString();	
			List fun = (List)info.get("userFunction");
			nodes = bs.getFuntion(functionDTO.getFunctionid(),roleid,type);
			Iterator it = nodes.iterator();
			while(it.hasNext()){
				FunctionDTO dto =(FunctionDTO)it.next(); 
				fun.add(dto.getHref());						//把功能点加入session中
				if("1".equals(dto.getType())){ //如果功能是按钮则过滤
					it.remove();
				}
			}
			info.remove("userFunction");
			info.put("userFunction", fun);
		}
		return "success";
	}
	/**
	 * 初始化功能点管理
	 * @return
	 * @throws ApplicationException
	 */
	public String initFunctionManager() throws ApplicationException{
		this.request("init", "true");
		return "initFunctionManager";
	}
	/**
	 * 功能点管理，取得全部功能点树形菜单
	 * @return
	 * @throws ApplicationException
	 */
	public String queryManagerFunction() throws ApplicationException{
		Map info = (Map)this.getSession().getAttribute("userinfo");
		String roleid=info.get("roleid").toString();
		String type = info.get("type").toString();	
		nodes = bs.getManagerFunction(functionDTO.getFunctionid(),roleid,type);
		return "success";
	}
	/**
	 * 修改功能点
	 * @return
	 * @throws ApplicationException
	 */
	public String modifyFunction() throws ApplicationException{
		functionDTO = bs.getFunctionDTO(functionDTO.getFunctionid());
		return "modifyFunction";
	}
	/**
	 * 保存功能点
	 * @return
	 * @throws ApplicationException
	 */
	public String saveFunction() throws ApplicationException{
		if(null==functionDTO.getFunctionid()||"".equals(functionDTO.getFunctionid())){
			funMethod="add";
		}else{
			funMethod="update";
		}
		bs.saveOrUpdateFunction(functionDTO);
		funMethod=funMethod+";"+functionDTO.getFunctionid();
		return "success";
	}
	/**
	 * 添加子功能点
	 * @return
	 * @throws ApplicationException
	 */
	public String addSubFunction()throws ApplicationException{
		String parentid = functionDTO.getFunctionid();
		functionDTO = new FunctionDTO();
		functionDTO.setParentid(parentid);
		return "modifyFunction";
	}
	/**
	 * 删除功能点
	 * @return
	 * @throws ApplicationException
	 */
	public String delFunction()throws ApplicationException{
		int flag = bs.delFunction(functionDTO.getFunctionid());
		if(flag<0){
			funMethod="error";
		}else{
			funMethod="success";
		}
		return "success";
	}
	
	public String queryMainMenu()throws ApplicationException{
		Map info = (Map)this.getSession().getAttribute("userinfo");
		String roleid=info.get("roleid").toString();
		String type = info.get("type").toString();	
		nodes = bs.getFuntion(null,roleid,type);
		return "returnTop";
	}
	
	/*
	 * ================================get set方法=====================================
	 */	
	@JSON(serialize=false)
	public IFunctionBS getBs() {
		return bs;
	}
	public void setBs(IFunctionBS bs) {
		this.bs = bs;
	}
	@JSON(serialize=false)
	public FunctionDTO getFunctionDTO() {
		return functionDTO;
	}

	public void setFunctionDTO(FunctionDTO functionDTO) {
		this.functionDTO = functionDTO;
	}

	public Object getModel() {
		return functionDTO;
	}
	public List getNodes() {
		return nodes;
	}

	public void setNodes(List nodes) {
		this.nodes = nodes;
	}

	public String getFunMethod() {
		return funMethod;
	}

	public void setFunMethod(String funMethod) {
		this.funMethod = funMethod;
	}
}
